package com.som.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.som.console.CfgEnum;
import com.som.console.ExcelProcException;
import com.som.console.PathEnum;
import com.som.service.ExcelDataServiceInf;
import com.som.service.excel.ExcelReadProcess;
import com.som.service.excel.ExcelReadWriteProcess;
import com.som.service.excel.data3.input.IpAddressInfoExcelRead;
import com.som.service.excel.data3.input.IpMaskInfoExcelRead;
import com.som.service.excel.data3.input.IpSectionInfoExcelRead;
import com.som.service.excel.data3.output.IpAddressInfoExcelWrite;
import com.som.service.excel.data3.output.IpSectionInfoExcelWrite;
import com.som.service.excel.data3.output.ipMaskInfoExcelWrite;
import com.som.service.excel.data3.state.StateContext;

/**
 * 需求三：IP地址格式互相转化
 * 
 * @author liujun
 * @date 2016年4月8日
 * @verion 0.0.1
 */
public class ExcelData3ServiceImpl implements ExcelDataServiceInf
{
	/**
	 * 用来存放所有输入的操作类信息
	 */
	@SuppressWarnings("rawtypes")
	private Map<String, ExcelReadProcess> inputMap = new HashMap<String, ExcelReadProcess>();

	/**
	 * 用来存放所有输出的操作类信息
	 */
	@SuppressWarnings("rawtypes")
	private Map<String, ExcelReadWriteProcess> outputMap = new HashMap<String, ExcelReadWriteProcess>();

	public ExcelData3ServiceImpl()
	{
		// 加入读取ip格式的操作类
		inputMap.put(CfgEnum.SYSTEM_FORMAT_TYPE_IP.getCfgMsg(), new IpAddressInfoExcelRead());
		// 加入读取ip段格式的操作类
		inputMap.put(CfgEnum.SYSTEM_FORMAT_TYPE_IPSECTION.getCfgMsg(), new IpSectionInfoExcelRead());
		// 加入读取ip掩码格式的操作类
		inputMap.put(CfgEnum.SYSTEM_FORMAT_TYPE_IPMASK.getCfgMsg(), new IpMaskInfoExcelRead());

		// 加入写入IP格式的操作类
		outputMap.put(CfgEnum.SYSTEM_FORMAT_TYPE_IP.getCfgMsg(), new IpAddressInfoExcelWrite());
		// 写入IP段格式的操作类
		outputMap.put(CfgEnum.SYSTEM_FORMAT_TYPE_IPSECTION.getCfgMsg(), new IpSectionInfoExcelWrite());
		// 写入IP掩码格式的操作类
		outputMap.put(CfgEnum.SYSTEM_FORMAT_TYPE_IPMASK.getCfgMsg(), new ipMaskInfoExcelWrite());

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean excelDataProcess(Map<String, String> pathMap) throws ExcelProcException
	{
		// 读取文件类型
		String inputType = pathMap.get(PathEnum.DATA3_INPUT_FILE_TYPE.getPathFlag());
		// 输出文件类型
		String outputType = pathMap.get(PathEnum.DATA3_OUTPUT_FILE_TYPE.getPathFlag());

		// 进行输入文件处理
		ExcelReadProcess read = inputMap.get(inputType);
		List list = read.readExcelDataList(pathMap.get(PathEnum.DATA3_INPUT_FILE_FLAG.getPathFlag()), (Integer.parseInt(inputType) - 1), true);

		// 进行转换的数据的模式处理
		StateContext contex = new StateContext();

		List procRspList = null;

		int sheetIndex = 0;

		// 由输出文件的类型决定调用哪种处理方法
		// 如果当前的输出为IP
		if (CfgEnum.SYSTEM_FORMAT_TYPE_IP.getCfgMsg().equals(outputType))
		{
			procRspList = contex.parseIp(inputType, list);
			sheetIndex = 0;
		}
		// 如果当前的输出为IP段
		else if (CfgEnum.SYSTEM_FORMAT_TYPE_IPSECTION.getCfgMsg().equals(outputType))
		{
			procRspList = contex.parseIpSection(inputType, list);
			sheetIndex = 1;
		}
		// 如果当前的输出为IP掩码
		else if (CfgEnum.SYSTEM_FORMAT_TYPE_IPMASK.getCfgMsg().equals(outputType))
		{
			procRspList = contex.parseIpMask(inputType, list);
			sheetIndex = 2;
		}

		// 进行输出文件操作
		ExcelReadWriteProcess writeOutpu = outputMap.get(outputType);
		writeOutpu.readWriteExcelByList(pathMap.get(PathEnum.DATA3_OUTPUT_FILE_FLAG.getPathFlag()), sheetIndex, true, procRspList);

		return true;
	}

}
